<?php
/**
*
* @package shopBB
* @copyright (c) 2008 EXreaction, Lithium Studios
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

if (!defined('IN_PHPBB'))
{
	exit;
}

class shop_plugins
{
	public static $plugins = array();
	public static $available_plugins = array();
	private static $to_do = array();
	private static $plugins_path = array();

	/**
	* Constructor
	*
	* Load all installed and enabled plugins
	*/
	public function __construct($shop_plugins_path = false)
	{
		global $cache, $config, $db, $table_prefix, $user;

		// Just in case it is not set we will use the default.
		if ($shop_plugins_path)
		{
			self::$plugins_path = $shop_plugins_path;
		}
		else
		{
			self::$plugins_path = SHOPBB_PATH . 'plugins/';
		}

		if (($cache_data = $cache->get('_shop_plugins')) === false)
		{
			if (!defined('SHOP_PLUGINS_TABLE'))
			{
				include($phpbb_root_path . 'shop/constants.' . $phpEx);
			}
			$sql = 'SELECT * FROM ' . SHOP_PLUGINS_TABLE . ' ORDER BY plugin_id ASC';
			$result = $db->sql_query($sql);
			while($row = $db->sql_fetchrow($result))
			{
				self::$plugins[$row['plugin_name']] = $row;
			}

			$cache->put('_shop_plugins', self::$plugins);
		}
		else
		{
			self::$plugins = $cache_data;
		}
		unset($cache_data);

		foreach (self::$plugins as $row)
		{
			if ($row['plugin_enabled'] && file_exists(self::$plugins_path . 'info/info_' . $row['plugin_name'] . '.' . $phpEx))
			{
				include(self::$plugins_path . 'info/info_' . $row['plugin_name'] . '.' . $phpEx);
			}
		}

		return true;
	}

	/**
	* Load all available plugins
	*/
	public static function load_all_plugins()
	{
		global $cache, $config, $db, $table_prefix, $user;

		$dh = @opendir(self::$plugins_path . 'info/');

		if ($dh)
		{
			while (($file = readdir($dh)) !== false)
			{
				if (strpos($file, 'info_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx)
				{
					$name = substr($file, 5, -(strlen($phpEx) + 1));

					if (!array_key_exists($name, self::$available_plugins))
					{
						self::$available_plugins[$name] = array();

						include(self::$plugins_path . 'info/info_' . $name . '.' . $phpEx);
					}
				}
			}

			closedir($dh);
		}

		return true;
	}

	public static function add_to_do($to_do)
	{
		foreach($to_do as $do => $what)
		{
			if (!array_key_exists($do, self::$to_do))
			{
				self::$to_do[$do] = $what;
			}
			else
			{
				self::$to_do[$do] = array_merge(self::$to_do[$do], $what);
			}
		}
	}

	public static function plugin_do($what, $args = array())
	{
		if (isset(self::$to_do[$what]))
		{
			foreach (self::$to_do[$what] as $function_name)
			{
				$function_name($args);
			}
		}

		return $args;
	}

	public static function plugin_do_ref($what, &$args)
	{
		if (isset(self::$to_do[$what]))
		{
			foreach (self::$to_do[$what] as $function_name)
			{
				$function_name($args);
			}
		}
	}

	/**
	* Install a plugin
	*/
	public static function plugin_install($which)
	{
		global $auth, $auth_admin, $cache, $config, $db, $dbmd, $dbms, $db_tool, $table_prefix, $user;

		if (!array_key_exists($which, self::$available_plugins))
		{
			trigger_error('PLUGIN_NOT_EXIST');
		}

		if (array_key_exists($which, self::$plugins))
		{
			trigger_error('PLUGIN_ALREADY_INSTALLED');
		}

		include(PHPBB_ROOT_PATH . 'includes/functions_install.' . PHP_EXT);
		include(PHPBB_ROOT_PATH . 'includes/db/db_tools.' . PHP_EXT);
		include(PHPBB_ROOT_PATH . 'includes/acp/auth.' . PHP_EXT);
		include(SHOPBB_PATH . 'install/functions.' . PHP_EXT);
		include(SHOPBB_PATH . 'install/eami.' . PHP_EXT);
		$auth_admin = new auth_admin();
		$db_tool = new phpbb_db_tools($db);
		$dbmd = get_available_dbms($dbms);
		$eami = new eami();

		if (file_exists(self::$plugins_path . $which . '/install.' . $phpEx))
		{
			include(self::$plugins_path . $which . '/install.' . $phpEx);
		}

		$sql_data = array(
			'plugin_name'		=> $which,
			'plugin_enabled'	=> 1,
			'plugin_version'	=> self::$available_plugins[$which]['plugin_version'],
		);

		$sql = 'INSERT INTO ' . SHOP_PLUGINS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data);
		$db->sql_query($sql);
		self::$plugins[$which] = $sql_data;

		add_log('admin', 'LOG_SHOP_PLUGIN_INSTALLED', $which);

		$cache->purge();
	}

	/**
	* Uninstall a plugin
	*/
	public static function plugin_uninstall($which)
	{
		global $auth, $auth_admin, $cache, $config, $db, $dbmd, $dbms, $db_tool, $table_prefix, $user;

		if (!array_key_exists($which, self::$plugins))
		{
			trigger_error('PLUGIN_NOT_INSTALLED');
		}

		include(PHPBB_ROOT_PATH . 'includes/functions_install.' . PHP_EXT);
		include(PHPBB_ROOT_PATH . 'includes/db/db_tools.' . PHP_EXT);
		include(PHPBB_ROOT_PATH . 'includes/acp/auth.' . PHP_EXT);
		include(SHOPBB_PATH . 'install/functions.' . PHP_EXT);
		include(SHOPBB_PATH . 'install/eami.' . PHP_EXT);
		$auth_admin = new auth_admin();
		$db_tool = new phpbb_db_tools($db);
		$dbmd = get_available_dbms($dbms);
		$eami = new eami();

		if (file_exists(self::$plugins_path . $which . '/uninstall.' . $phpEx))
		{
			include(self::$plugins_path . $which . '/uninstall.' . $phpEx);
		}

		$sql = 'DELETE FROM ' . SHOP_PLUGINS_TABLE . ' WHERE plugin_name = \'' . $db->sql_escape($which) . '\'';
		$db->sql_query($sql);

		unset(self::$plugins[$which]);

		add_log('admin', 'LOG_SHOP_PLUGIN_UNINSTALLED', $which);

		$cache->purge();
	}

	/**
	* Update a plugin
	*/
	public static function plugin_update($which)
	{
		global $auth, $auth_admin, $cache, $config, $db, $dbmd, $dbms, $db_tool, $table_prefix, $user;

		if (!array_key_exists($which, self::$plugins))
		{
			trigger_error('PLUGIN_NOT_INSTALLED');
		}

		$newer_files = false;
		if (self::$available_plugins[$which]['plugin_version'] != self::$plugins[$which]['plugin_version'])
		{
			$version = array('files' => explode('.', self::$available_plugins[$which]['plugin_version']), 'db' => explode('.', self::$plugins[$which]['plugin_version']));

			$i = 0;
			foreach ($version['files'] as $v)
			{
				if ($v > $version['db'][$i])
				{
					$newer_files = true;
					break;
				}
				else if ($v < $version['db'][$i])
				{
					break;
				}
				$i++;
			}
		}

		if ($newer_files)
		{
			include(PHPBB_ROOT_PATH . 'includes/functions_install.' . PHP_EXT);
			include(PHPBB_ROOT_PATH . 'includes/db/db_tools.' . PHP_EXT);
			include(PHPBB_ROOT_PATH . 'includes/acp/auth.' . PHP_EXT);
			include(SHOPBB_PATH . 'install/functions.' . PHP_EXT);
			include(SHOPBB_PATH . 'install/eami.' . PHP_EXT);
			$auth_admin = new auth_admin();
			$db_tool = new phpbb_db_tools($db);
			$dbmd = get_available_dbms($dbms);
			$eami = new eami();

			$current_version = self::$plugins[$which]['plugin_version'];

			if (file_exists(self::$plugins_path . $which . '/update.' . $phpEx))
			{
				include(self::$plugins_path . $which . '/update.' . $phpEx);
			}

			$sql = 'UPDATE ' . SHOP_PLUGINS_TABLE . ' SET plugin_version = \'' . self::$available_plugins[$which]['plugin_version'] . '\' WHERE plugin_name = \'' . $db->sql_escape($which) . '\'';
			$db->sql_query($sql);

			self::$plugins[$which]['plugin_version'] = self::$available_plugins[$which]['plugin_version'];

			add_log('admin', 'LOG_SHOP_PLUGIN_UPDATED', $which);

			$cache->destroy('_shop_plugins');
		}
	}

	/**
	* Enable a plugin
	*/
	public static function plugin_enable($which)
	{
		global $db;

		if (!array_key_exists($which, self::$plugins))
		{
			self::plugin_install($which);
			return;
		}

		$sql = 'UPDATE ' . SHOP_PLUGINS_TABLE . ' SET plugin_enabled = 1 WHERE plugin_name = \'' . $db->sql_escape($which) . '\'';
		$db->sql_query($sql);

		self::$plugins[$which]['plugin_enabled'] = 1;

		add_log('admin', 'LOG_SHOP_PLUGIN_ENABLED', $which);

		$cache->destroy('_shop_plugins');
	}

	/**
	* Disable a plugin
	*/
	public static function plugin_disable($which)
	{
		global $db;

		if (!array_key_exists($which, self::$plugins))
		{
			self::plugin_install($which);
		}

		$sql = 'UPDATE ' . SHOP_PLUGINS_TABLE . ' SET plugin_enabled = 0 WHERE plugin_name = \'' . $db->sql_escape($which) . '\'';
		$db->sql_query($sql);

		self::$plugins[$which]['plugin_enabled'] = 0;

		add_log('admin', 'LOG_SHOP_PLUGIN_DISABLED', $which);

		$cache->destroy('_shop_plugins');
	}

	/**
	* Move a plugin
	*
	* This is used to change the priority of a plugin
	*/
	public static function plugin_move($which, $action)
	{
		global $cache, $db, $phpEx;

		if (!array_key_exists($which, self::$plugins))
		{
			trigger_error('PLUGIN_NOT_INSTALLED');
		}

		$temp = self::$plugins;
		if ($action == 'move_down')
		{
			$temp = array_reverse($temp);
		}

		$to = $to_id = false;
		foreach ($temp as $plugin_name => $data)
		{
			if ($plugin_name == $which)
			{
				break;
			}
			$to_id = $data['plugin_id'];
		}

		if ($to_id)
		{
			$sql = 'UPDATE ' . SHOP_PLUGINS_TABLE . ' SET plugin_id = 0 WHERE plugin_name = \'' . $db->sql_escape($which) . '\'';
			$db->sql_query($sql);

			$sql = 'UPDATE ' . SHOP_PLUGINS_TABLE . ' SET plugin_id = ' . self::$plugins[$which]['plugin_id'] . ' WHERE plugin_id = ' . $to_id;
			$db->sql_query($sql);

			$sql = 'UPDATE ' . SHOP_PLUGINS_TABLE . ' SET plugin_id = ' . $to_id . ' WHERE plugin_id = 0';
			$db->sql_query($sql);
		}
		unset($temp);

		$cache->destroy('_shop_plugins');
	}
}
?>